﻿using UnityEngine;
using System.Collections;

public class AmmoBills : MonoBehaviour {

    int m_iTotalPrice;
	// Use this for initialization
	void OnEnable() {
        AmmoBox();

        m_iTotalPrice = 0;
        transform.FindChild("TotalPrice").GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
	}

    void OnDisable()
    {
        for (int i = 0; i < transform.childCount; ++i)
        {
            if (transform.GetChild(i).name == "AmmoChart(Clone)")
                Destroy(transform.GetChild(i).gameObject);
        }
    }
	
	// Update is called once per frame
	void Update () {
	
	}

    void ResetPage()
    {
        for (int i = 0; i < transform.childCount; ++i)
        {
            if (transform.GetChild(i).gameObject.name == "SupplyWpnBox(Clone)")
            {
                transform.GetChild(i).GetComponent<SupplyWpnBox>().Init();
                if (!transform.GetChild(i).GetComponent<SupplyWpnBox>().m_bExample)
                {
                    transform.GetChild(i).FindChild("SupplyBarPointer").localPosition = new Vector3(-3, 0);
                    transform.GetChild(i).FindChild("SupplyBarPointer").GetComponent<SupplyBarPointer>().BuyingObj_NumCheck();
                }
            }
        }
    }

    void OnClick() // Confirm Buy//////////////
    {
        if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney >= m_iTotalPrice)
        {
            for (int i = 0; i < transform.childCount; ++i)
            {
                if (transform.GetChild(i).gameObject.name == "SupplyWpnBox(Clone)")
                {
                    if (transform.GetChild(i).GetComponent<SupplyWpnBox>().m_iNumOfBuyingObj > 0)
                    {
                        for (int j = 0; j < transform.GetChild(i).GetComponent<SupplyWpnBox>().m_iNumOfBuyingObj; ++j)
                        {
                            GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList.Add(transform.GetChild(i).GetComponent<SupplyWpnBox>().m_SupplyWpn.m_WpnData);
                            transform.GetChild(i).GetComponent<SupplyWpnBox>().m_SupplyWpn.m_iNumberOfWpn--;
                        }

                    }
                }
                else if (transform.GetChild(i).gameObject.name == "AmmoChart(Clone)")
                {
                    if (!transform.GetChild(i).GetComponent<AmmoChart>().m_bExample)
                    {
                        transform.GetChild(i).GetComponent<AmmoChart>().BuyAmmo();
                    }
                }
            }

            for (int i = 0; i < transform.childCount; ++i)
            {
                if (transform.GetChild(i).name == "AmmoChart(Clone)")
                    Destroy(transform.GetChild(i).gameObject);
            }


            GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurWinNum = 0;

            ResetPage();
            GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney -= m_iTotalPrice;
            GameObject.Find("ShopWdw(Clone)").GetComponent<ShopWdw>().SwipeToRightWdw();
            GameTime.TimeSpeed_Play();
        }
        else
        {
            GameObject WarnMsg = Resources.Load("Prefaps/UI/StaticUI/Warn/WarnMessageLabel") as GameObject;
            (Instantiate(WarnMsg) as GameObject).transform.parent = transform.parent;
        }
    }

    public void CalculateTotalPrice()
    {
        m_iTotalPrice = 0;

        for (int i = 0; i < transform.childCount; ++i)
        {
            if (transform.GetChild(i).gameObject.name == "AmmoChart(Clone)")
            {
                m_iTotalPrice += transform.GetChild(i).GetComponent<AmmoChart>().m_iThisPrice;
            }
        }

        transform.FindChild("TotalPrice").GetComponent<UILabel>().text = string.Format("$ {0}", m_iTotalPrice);
    }

    void AmmoBox()
    {
        if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().AmmoDumpList.Count == 0)
        {
            transform.FindChild("TotalPrice").GetComponent<UILabel>().text = "You need at least one of 'AmmoDump'";
            transform.FindChild("TotalPrice").localPosition = new Vector3(-1.7f, -2.86f);
            transform.FindChild("TotalPrice").GetComponent<UILabel>().overflowMethod = UILabel.Overflow.ResizeFreely;
        }
        else
        {
            transform.FindChild("TotalPrice").GetComponent<UILabel>().text = "$ 0";
            transform.FindChild("TotalPrice").localPosition = new Vector3(-7f, -45.8f);
            transform.FindChild("TotalPrice").GetComponent<UILabel>().overflowMethod = UILabel.Overflow.ShrinkContent;

            for (int i = 0; i < GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().AmmoDumpList.Count + 1; ++i)
            {
                (Instantiate(Resources.Load("Prefaps/UI/Paper/AmmoBills/AmmoChart") as GameObject) as GameObject).transform.parent = transform;
                //transform.GetChild(i + 1).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
                transform.GetChild(i + 1).localPosition = new Vector3(-1.35f, 25 - (i * 4.5f));
                transform.GetChild(i + 1).localScale = new Vector3(0.85f, 0.05f);
                if (i != 0)
                    transform.GetChild(i + 1).GetComponent<AmmoChart>().m_ammoBox = GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().AmmoDumpList[i - 1];
                else
                    transform.GetChild(i + 1).GetComponent<AmmoChart>().m_bExample = true;
                transform.GetChild(i + 1).GetComponent<AmmoChart>().Init();
            }
        }
    }
}
